// pages/map/map.js
const towxImg = require('../../../utils/towxImg');
Page({
  data: {
    longitude: "116.397469",
    latitude: "39.908821",
    markers: [],
    article: ""
  },
  regionchange(e) {
    console.log(e.type)
  },
  markertap(e) {
    console.log(e.markerId)
  },
  controltap(e) {
    console.log(e.controlId)
  },
  // 获取地理位置
  getLocation() {
    const that = this;
    wx.getLocation({
      type: "gcj02",
      success: (res) => {
        // console.log(res);
        wx.showToast({
          title: "已定位到当前位置",
          icon: "none",
        });
        that.setData({
          longitude: res.longitude,
          latitude: res.latitude,
        });
      },
      fail: (err) => {
        // console.log(err);
        wx.showToast({
          title: "位置获取失败",
          icon: "none",
        });
        // this.baiduMap("北京市故宫博物院");
        that.chooseLocation();
      },
    });
  },

  //   授权位置
  chooseLocation() {
    const that = this;
    wx.getSetting({
      success: (result) => {
        // console.log(result);
        if (
          result.authSetting["scope.userLocation"] != undefined &&
          result.authSetting["scope.userLocation"] != true
        ) {
          wx.showModal({
            title: "是否授权当前位置",
            content: "需要获取您的地理位置，请确认授权，否则地图功能将无法使用",
            success: function (res) {
              if (res.cancel) {
                wx.navigateBack();
                wx.showToast({
                  title: "授权失败",
                  icon: "error",
                });
              } else if (res.confirm) {
                wx.openSetting({
                  success: function (data) {
                    // console.log(data);
                    if (data.authSetting["scope.userLocation"] == true) {
                      wx.showToast({
                        title: "授权成功",
                        icon: "success",
                      });
                      //再次授权，调用getLocationt的API
                      that.getLocation();
                    } else {
                      wx.showToast({
                        title: "授权失败",
                        icon: "none",
                      });
                    }
                  },
                });
              }
            },
          });
        }
      },
      fail: () => { },
      complete: () => { },
    });
  },

  onLoad(options) {
    this.getLocation();

    let code = '```\n<view class="my-page">\n  <map longitude="{{longitude}}" latitude="{{latitude}}" show-location="true" markers="{{markers}}" bindcontroltap="controltap" bindmarkertap="makertap" bindregionchange="regionchange" style="width: 100vw; height: 50vh;" />\n</view>\n```\n\n```\nPage({\n  data: {\n    longitude: "116.397469",\n    latitude: "39.908821",\n    markers: [],\n  },\n  regionchange(e) {\n    console.log(e.type)\n  },\n  markertap(e) {\n    console.log(e.markerId)\n  },\n  controltap(e) {\n    console.log(e.controlId)\n  },\n  // 获取地理位置\n  getLocation() {\n    const that = this;\n    wx.getLocation({\n      type: "gcj02",\n      success: (res) => {\n        // console.log(res);\n        wx.showToast({\n          title: "已定位到当前位置",\n          icon: "none",\n        });\n        that.setData({\n          longitude: res.longitude,\n          latitude: res.latitude,\n        });\n      },\n      fail: (err) => {\n        // console.log(err);\n        wx.showToast({\n          title: "位置获取失败",\n          icon: "none",\n        });\n        // this.baiduMap("北京市故宫博物院");\n        that.chooseLocation();\n      },\n    });\n  },\n  //   授权位置\n  chooseLocation() {\n    const that = this;\n    wx.getSetting({\n      success: (result) => {\n        // console.log(result);\n        if (\n          result.authSetting["scope.userLocation"] != undefined &&\n          result.authSetting["scope.userLocation"] != true\n        ) {\n          wx.showModal({\n            title: "是否授权当前位置",\n            content: "需要获取您的地理位置，请确认授权，否则地图功能将无法使用",\n            success: function (res) {\n              if (res.cancel) {\n                wx.navigateBack();\n                wx.showToast({\n                  title: "授权失败",\n                  icon: "error",\n                });\n              } else if (res.confirm) {\n                wx.openSetting({\n                  success: function (data) {\n                    // console.log(data);\n                    if (data.authSetting["scope.userLocation"] == true) {\n                      wx.showToast({\n                        title: "授权成功",\n                        icon: "success",\n                      });\n                      //再次授权，调用getLocationt的API\n                      that.getLocation();\n                    } else {\n                      wx.showToast({\n                        title: "授权失败",\n                        icon: "none",\n                      });\n                    }\n                  },\n                });\n              }\n            },\n          });\n        }\n      },\n      fail: () => { },\n      complete: () => { },\n    });\n  },\n  onLoad(options) {\n    this.getLocation();\n  },\n})\n```\n';

    let article = towxImg.dealWithRichText(code);
    this.setData({ article });
  },
})